package com.caldremch.incrementalupdates;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;
import utils.ApkExtract;
import utils.BsPatch;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.button)
    Button button;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    @BindView(R.id.text2)
    TextView text2;
    @BindView(R.id.button2)
    Button button2;
    @BindView(R.id.msg)
    TextView msg;
    private String apkPath;
    private String filePath;

    public final static String TAG = "caldremch";
    public final static String PATCH_FILE = "PATCH.patch";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        apkPath = ApkExtract.extract(this);
        filePath = Environment.getExternalStorageDirectory().getAbsolutePath();

        msg.setText("filePath=" + filePath + "\n" + "apkPath=" + apkPath);

        Log.e(TAG, "filePath=" + filePath);
        Log.e(TAG, "apkPath=" + apkPath);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                OkGo.get("http://192.168.0.106:8080/AndroidWebViewTest/old-to-new.patch")
                        .tag(MainActivity.this)
                        .execute(new FileCallback(filePath, PATCH_FILE) {
                            @Override
                            public void onSuccess(File file, Call call, Response response) {

                                Log.e(TAG, file.getAbsolutePath());

                            }

                            @Override
                            public void downloadProgress(long currentSize, long totalSize, float progress, long networkSpeed) {

                                text2.setText("下载进度：" + progress);

                            }
                        });

            }
        });


        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final File patch = new File(filePath, PATCH_FILE);
                final File oldApk = new File(apkPath);
                final File newApk = new File(filePath, "new.apk");


//                try {
                                    /*合成新的apk*/
                BsPatch.bspatch(ApkExtract.extract(MainActivity.this), newApk.getAbsolutePath(), patch.getAbsolutePath());
                /*安装合成的新apk*/
                ApkExtract.install(MainActivity.this, newApk.getAbsolutePath());

//                } catch (Exception e) {

//                    Log.e(TAG, e.getMessage());
//                    e.printStackTrace();
//
//                }

            }
        });


//        1.下载path文件， 下载完成后提示安装


    }
}
